package com.price.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.animation.Animation
import android.view.animation.Transformation
import com.price.common.extend.logError
import com.zp.common.R

class LineProgressBar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private val mPaint: Paint// 画笔
    private val anim: CircleBarAnim// 动画
    private var progressWidth = 0f //进度条长度度
    // 以下是自定义参数
    private var mAnnulusColor = Color.GRAY // 圆环颜色
    private var mLoadColor = Color.BLACK // 加载进度圆弧扫过的颜色

    private var mProgress = 0 // 当前进度
    private val maxProgress = 100 // 最大进度，默认100

    private val progressRectF:RectF=RectF()


    init {
        // 获取自定义属性
        val value = context.theme.obtainStyledAttributes(
            attrs, R.styleable.LineProgressView, defStyleAttr, 0
        )
        val indexCount = value.indexCount
        for (i in 0 until indexCount) {
            val parm = value.getIndex(i)
            when (parm) {
                R.styleable.LineProgressView_progressBg -> mAnnulusColor =
                    value.getColor(parm, Color.BLACK)

                R.styleable.LineProgressView_progressColor -> mLoadColor =
                    value.getColor(parm, Color.BLACK)

                R.styleable.LineProgressView_progress -> mProgress = value.getInt(parm, 10)
            }
        }
        value.recycle()
        mPaint = Paint()
        // 取消锯齿
        mPaint.isAntiAlias = true
        // 设置空心
        mPaint.style = Paint.Style.FILL
        mPaint.strokeCap = Paint.Cap.ROUND

        // 动画
        anim = CircleBarAnim()
        startAnimation(anim)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        mPaint.strokeWidth=height.toFloat()

        // 获取圆形坐标
        val centerV = height / 2f
        
        // 设置画笔颜色
        mPaint.color = mAnnulusColor

        canvas.drawLine(centerV,centerV,width.toFloat()-centerV,centerV,mPaint)

        // TODO:画进度
        if(progressWidth>0) {
            // 设置进度颜色
            mPaint.color = mLoadColor
            progressRectF.top=0f
            progressRectF.left=0f
            progressRectF.bottom=height.toFloat()
            progressRectF.right=progressWidth
            canvas.drawRoundRect(progressRectF,centerV,centerV,mPaint)
        }
    }

    fun setProgressColor(color:Int){
        mLoadColor=color
    }
    /**
     * 设置进度
     * @param progress 进度值
     * @param time 动画时间范围
     */
    @Synchronized
    fun setProgress(progress: Int, time: Int = 1000) {
        anim.duration = time.toLong()
        startAnimation(anim)
        mProgress = progress
    }

    // 动画
    inner class CircleBarAnim : Animation() {
        override fun applyTransformation(interpolatedTime: Float, t: Transformation) {
            super.applyTransformation(interpolatedTime, t)
            progressWidth = interpolatedTime * (mProgress * width / maxProgress) //这里计算进度条的比例
            postInvalidate()
        }
    }

    @Synchronized
    fun getProgress(): Int {
        return mProgress
    }
}